package com.mobileagri.backoffice.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.mobileagri.backoffice.model.AbstractObject;
import com.mobileagri.backoffice.model.Utilisateur;

/**
 * 
 * @author LOUVET Romain
 * @param <E>
 * 
 */
@Repository
public class UtilisateurDAO<E> extends AbstractDAO implements IDAO {

	private HibernateTemplate hibernateTemplate;

	private SessionFactory sessionFactory;

	/**
	 * Get List of Message from database
	 * 
	 * @return list of all Message
	 */
	public List<Utilisateur> getAll(int start, int limit) {

		List<Utilisateur> listUser = new ArrayList<Utilisateur>();
		List<Object> listObject = super.getObjects(new Utilisateur(), start,
				limit);
		Utilisateur u;
		for (Object o : listObject) {
			u = (Utilisateur) o;

			if (u.getPrivilege() == 2)
				listUser.add(u);
		}

		return listUser;
	}

	/**
	 * Get List of contacts from database
	 * 
	 * @return list of all contacts
	 */
	@SuppressWarnings("unchecked")
	public List<E> getAll() {
		String sqlQuery = " from Utilisateur";

		getHibernateTemplate().find(sqlQuery);
		return (List<E>) getHibernateTemplate().find(sqlQuery);
	}

	/**
	 * Delete a contact with the id passed as parameter
	 * 
	 * @param id
	 */
	public void delete(int id) {
		super.delete(new Utilisateur(), id);
	}

	/**
	 * Create a new Contact on the database or Update contact
	 * 
	 * @param contact
	 * @return contact added or updated in DB
	 */
	public AbstractObject save(AbstractObject o) {
		Utilisateur user = (Utilisateur) o;
		return (Utilisateur) super.save(user);
	}

	/**
	 * Get total of Message from database
	 * 
	 * @return
	 */
	public int getTotal() {
		return DataAccessUtils.intResult(getHibernateTemplate().find(
				"SELECT COUNT(*) FROM Utilisateur"));
	}

	public Double getAvancement(String userName) {

		String annee = userName.substring(userName.length() - 4,
				userName.length());
		Integer anneeInt = new Integer(annee) - 1;

		String name = userName.substring(0, userName.length() - 4);

		System.out.println("name:" + name);

		// int nbDeb = DataAccessUtils.intResult(getHibernateTemplate().find(
		// "SELECT count(*) FROM donnees d WHERE d.user = 'duyck2012'"));
		int nbDeb = DataAccessUtils.intResult(getHibernateTemplate().find(
				"SELECT count(*) FROM Donnees d WHERE d.user = '" + name
						+ anneeInt.toString() + "'"));

		System.out.println("nbDeb : " + nbDeb);

		// int nbFin = DataAccessUtils.intResult(getHibernateTemplate().find(
		// "SELECT count(*) FROM donnees d WHERE d.user = 'duyck2013'"));
		int nbFin = DataAccessUtils.intResult(getHibernateTemplate().find(
				"SELECT count(*) FROM Donnees d WHERE d.user = '" + userName
						+ "'"));

		if (nbDeb == 0)
			return 0.0;

		System.out.println("nbFin : " + nbFin);
		System.out.println("nbFin / nbDeb : " + nbFin / nbDeb);

		return (double) nbFin / nbDeb;
	}
}
